package com.huatai.user.controller;

import com.huatai.common.api.R;
import com.huatai.common.util.ForestNodeMerger;
import com.huatai.user.entity.Role;
import com.huatai.user.service.RoleService;
import com.huatai.user.vo.PermissionVo;
import com.huatai.user.vo.RolePermissuinParamVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @program: ht-system
 * @description: 角色控制器
 * @author: songxiude
 * @create: 2023-02-09 13:21
 **/
@RestController
@RequestMapping("/role")
@Api(value = "角色管理", tags = "角色管理")
public class RoleController {
	@Autowired
	private RoleService roleService;

	@PostMapping("/saveRole")
	@ApiOperation(value = "角色新增保存",notes = "角色新增保存")
	public R saveRole(@Valid @RequestBody Role role){
		return R.status(roleService.saveRole(role));

	}
	@GetMapping("/getRoleList")
	@ApiOperation(value = "获取角色列表",notes = "获取角色列表")
	public R<List<Role>> getRoleList(){
		return R.data(roleService.getRoleList());
	}

	@PostMapping("/getRolePermissions")
	@ApiOperation(value = "获取角色资源列表",notes = "获取角色资源列表")
	public R<List<PermissionVo>> getRolePermissions(@ApiParam(name = "角色id",value = "role_id",required = true) @RequestParam("role_id") Long roleId){
		return R.data(roleService.getRolePermissions(roleId));
	}

	@PostMapping("/rolePermissionEdit")
	@ApiOperation(value = "角色分配资源权限",notes = "角色分配资源权限")
	public R rolePermissionEdit(@Valid @RequestBody RolePermissuinParamVo rolePermissuinParamVo){
		return R.status(roleService.rolePermissionEdit(rolePermissuinParamVo));
	}

	@PostMapping("/delRole")
	@ApiOperation(value = "删除角色",notes = "删除角色")
	public R delRole(@ApiParam(name = "role_id", value = "角色id", required = true) @NotBlank(message = "角色id不能为空") @RequestParam("role_id") Long roleId){
		return R.status(roleService.delRole(roleId));
	}
}
